﻿#region

using System;
using System.IO;
using System.Web.Mvc;
using System.Web.WebPages;
using HouseHoldBudget.Core;
using HouseHoldBudget.Core.Infrastructure;

#endregion

namespace HouseHoldBudget.Framework.Mvc
{
    public abstract class WebViewPage<TModel> : System.Web.Mvc.WebViewPage<TModel>
    {
        #region Fields

        private IWorkerContext _workContext;

        #endregion

        #region Instance Properties

        public override string Layout
        {
            get
            {
                var layout = base.Layout;

                if (!string.IsNullOrEmpty(layout))
                {
                    var filename = Path.GetFileNameWithoutExtension(layout);
                    ViewEngineResult viewResult = ViewEngines.Engines.FindView(ViewContext.Controller.ControllerContext, filename, "");

                    if (viewResult.View != null && viewResult.View is RazorView)
                    {
                        layout = (viewResult.View as RazorView).ViewPath;
                    }
                }

                return layout;
            }
            set
            {
                base.Layout = value;
            }
        }

        public IWorkerContext WorkContext
        {
            get
            {
                return _workContext;
            }
        }

        #endregion

        #region Instance Methods

        public override void InitHelpers()
        {
            base.InitHelpers();

            _workContext = EngineContext.Current.Resolve<IWorkerContext>();
        }

        public HelperResult RenderSection(string sectionName, Func<object, HelperResult> defaultContent)
        {
            return IsSectionDefined(sectionName) ? RenderSection(sectionName) : defaultContent(new object());
        }

        public HelperResult RenderWrappedSection(string name, object wrapperHtmlAttributes)
        {
            Action<TextWriter> action = delegate(TextWriter tw)
                                            {
                                                var htmlAttributes = HtmlHelper.AnonymousObjectToHtmlAttributes(wrapperHtmlAttributes);
                                                var tagBuilder = new TagBuilder("div");
                                                tagBuilder.MergeAttributes(htmlAttributes);

                                                var section = RenderSection(name, false);
                                                if (section != null)
                                                {
                                                    tw.Write(tagBuilder.ToString(TagRenderMode.StartTag));
                                                    section.WriteTo(tw);
                                                    tw.Write(tagBuilder.ToString(TagRenderMode.EndTag));
                                                }
                                            };
            return new HelperResult(action);
        }

        #endregion
    }

    public abstract class WebViewPage : WebViewPage<dynamic>
    {
    }
}